package com.avecsys.quotemanager.mbeans.supplier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.icefaces.ace.event.SelectEvent;

import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.mbeans.buyer.QuoteEventReplyListMBean;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.QuoteTransactionPojo;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierUsersPojo;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.service.supplier.QuoteTransactionService;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.SelectItemUtils;

/**
 * @author Sam Sundar K
 */
public class SendQuoteToBuyerListMBean extends BaseMBean implements Serializable
{
	private QuoteTransactionService quoteTransactionService;

	private List<QuoteTransactionPojo> quoteTransactionPojoList = new ArrayList<QuoteTransactionPojo>();

	private List<SelectItem> eventTransactionCodesListAll = new ArrayList<SelectItem>();

	/**
	 * @return the eventTransactionCodesListAll
	 */
	public List<SelectItem> getEventTransactionCodesListAll()
	{
		return eventTransactionCodesListAll;
	}

	/**
	 * @param eventTransactionCodesListAll the eventTransactionCodesListAll to set
	 */
	public void setEventTransactionCodesListAll(List<SelectItem> eventTransactionCodesListAll)
	{
		this.eventTransactionCodesListAll = eventTransactionCodesListAll;
	}

	/**
	 * @return the quoteTransactionPojoList
	 */
	public List<QuoteTransactionPojo> getQuoteTransactionPojoList()
	{
		return quoteTransactionPojoList;
	}

	/**
	 * @return the quoteTransactionService
	 */
	public QuoteTransactionService getQuoteTransactionService()
	{
		return quoteTransactionService;
	}

	public void loadSendQuoteToBuyerList()
	{
		UserMasterPojo userMasterPojo = getSignedInUser();
		if (userMasterPojo != null)
		{
			for (SupplierUsersPojo supplierUsersPojo : userMasterPojo.getSupplierUsersPojo())
			{
				SupplierMasterPojo supplierMasterPojo = supplierUsersPojo.getSupplierMasterPojo();
				List<QuoteTransactionPojo> quoteTransactionPojoList;
				try
				{
					quoteTransactionPojoList = quoteTransactionService.getAllSentQuotes(supplierMasterPojo);
					setQuoteTransactionPojoList(quoteTransactionPojoList);
					setEventTransactionCodesListAll(SelectItemUtils.getAllEventTransactionStatusCodes());
				}
				catch (BusinessException e)
				{
					MessageBoardUtil.publishException(e);
				}
			}
		}
	}

	/**
	 * @param quoteTransactionPojoList the quoteTransactionPojoList to set
	 */
	public void setQuoteTransactionPojoList(List<QuoteTransactionPojo> quoteTransactionPojoList)
	{
		this.quoteTransactionPojoList = quoteTransactionPojoList;
	}

	/**
	 * @param quoteTransactionService the quoteTransactionService to set
	 */
	public void setQuoteTransactionService(QuoteTransactionService quoteTransactionService)
	{
		this.quoteTransactionService = quoteTransactionService;
	}

	public String showAllQuoteEvents(SelectEvent selectEvent)
	{
		MessageBoardUtil.resetMessageBoard();
		FacesContext faceContext = FacesContext.getCurrentInstance();
		QuoteTransactionPojo quoteTransactionPojo = (QuoteTransactionPojo) selectEvent.getObject();
		QuoteEventReplyListMBean quoteEventReplyListMBean = (QuoteEventReplyListMBean) faceContext.getApplication()
				.evaluateExpressionGet(
						faceContext,
							"#{quoteEventReplyListMBean}",
							QuoteEventReplyListMBean.class);
		quoteEventReplyListMBean.loadQuoteEventHistoryForSupplier(quoteTransactionPojo);
		return "showQuoteTransactionEventHistory";
	}
}
